<?php
class AppController extends Controller {
	var $name = 'App';
	var $components = array('Auth','Security','Bench','RequestHandler');
	var $auth_allow = array('*');
	var $auth_deny  = array();
		
	var $controller_title = '';
	var $pageTitle ='';
	
	//----------------------------------------------------------
	// ページタイトルの設定
	//----------------------------------------------------------
	function _PageTitle($str = '', $overwrite = false){

		if(!$overwrite){
			$this->pageTitle = SITE_NAME;
			if($this->pageTitle != '' && $this->controller_title != ''){
				$this->pageTitle .= ' : ';
			}
			
			$this->pagaTitle .= $this->controller_title;
			if($this->pageTitle != '' && $str != ''){
				$this->pageTitle .= ' : ';
			}
		}
		$this->set('title_for_layout', $str);
		
		//$this->pageTitle .= $str;
	}
	
	
	//----------------------------------------------------------------
	//beforeFilterコールバック
	//----------------------------------------------------------------
	function beforeFilter(){
		
		//動的設定値(Configration)を入手
		//
		$this->Configration = ClassRegistry::init('Configration');
		$data = $this->Configration->find('list', array(
			'fields' => array('name', 'value'), 
		));
        
		//サイトがオープンしていない場合は案内ページにリダイレクト
		//
		if(!$data['public_site_open']){
			if($this->name != 'Main' || $this->action != 'closed'){
				$this->redirect(array('controller' => 'main', 'action' =>'closed'));
			}
		}
		
		//設定値を保存
		//
		Configure::write('Configration', $data);
		
		//Userモデルが登録されていない場合は追加する
		if(!array_search('Users', $this->uses)){
			$this->uses[] = 'Users';
		}
		
		//Authコンポーネントの設定
		//
		$this->Auth->userModel = 'Users';
		$this->Auth->fields = array(
			'username' => 'shopid',
			'password' => 'password',
		);
		$this->Auth->userScope = array(
			'Users.registstatus =' => PUBLIC_REGIST_STATUS_REGIST
		);
		$this->Auth->loginAction = array(
			'controller' => 'users', 
			'action' => 'login'
		);
		$this->Auth->loginRedirect = array(
			'controller' => 'main', 
			'action' => 'main'
		);
		$this->Auth->logoutRedirect = array(
			'controller' => 'users', 
			'action' => 'logout'
		);
		$this->Auth->allow($this->auth_allow);
		$this->Auth->deny($this->auth_deny);
		$this->Auth->authError = 'ログインをしていません';
		$this->Auth->loginError = 'ショップIDもしくはパスワードﾞが間違っています';
		
        $this->Security->blackHoleCallback = 'securityerror';
        
	}
	
	//----------------------------------------------------------------
	//セキュリティーエラーアクション
	//----------------------------------------------------------------
	function securityerror(){
		
        $this->Bench->report('securityerror log start');
        $this->Bench->report('securityerror IP : '. $this->RequestHandler->getClientIP());
        $this->Bench->report('securityerror Referren : '.$this->referer());
        $this->Bench->report('securityerror log end');
        
		exit;
	}
	
}
